-
Notifications
You must be signed in to change notification settings - Fork 0
/
Singly Linked List - Bubble sort.cpp
70 lines (64 loc) · 1.41 KB
/
Singly Linked List - Bubble sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
struct NODE {
int data;
char txt;
NODE *next;
};
NODE *head, *tail, *temp;
void add(char txt, int value){
if(head == NULL){
head = new NODE;
head->txt = txt;
head->data = value;
head->next = 0;
tail = head;
} else {
tail->next = new NODE;
tail = tail->next;
tail->txt = txt;
tail->data = value;
tail->next = 0;
}
}
void bubbleSort_ASC(){
if(head == NULL){
cout << "Sorting not possible!" << endl;
} else {
for(NODE *x = head; x->next != 0; x = x->next){
for(NODE *y = head; y->next != 0; y = y->next){
if(y->data > y->next->data){
int swap = y->data;
y->data = y->next->data;
y->next->data = swap;
}
}
}
}
}
int print(NODE *ref){
if(ref == 0){
cout << "[EMPTY LIST]" << endl;
return false;
} else {
while(ref != 0){
cout << "[" << ref->txt << "-" << ref->data << "]";
ref = ref->next;
if(ref != 0 ) cout << " => ";
}
cout << endl;
return true;
}
}
int main(){
add('A', 100);
add('B', 10);
add('C', 50);
add('D', 15);
add('E', 30);
add('F', 70);
add('G', 20);
add('H', 40);
bubbleSort_ASC();
print(head);
}